package com.edu.zut.hb.software.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.zut.hb.software.server.pojo.RespBean;
import com.edu.zut.hb.software.server.pojo.Teacher;
import com.edu.zut.hb.software.server.pojo.UpdateVo.TeacherUpdateVo;
import com.edu.zut.hb.software.server.pojo.registerVo.TeacherRegisterVo;
import com.edu.zut.hb.software.server.service.ITeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2021-08-15
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    @ApiOperation(value = "获取所有老师")
    @GetMapping("/")
    public List<Teacher> getAllTeacher() {
        return teacherService.list(new QueryWrapper<Teacher>().last("limit 200"));
    }

    @ApiOperation(value = "添加老师")
    @PostMapping("/add")
    public RespBean addTeacher(@RequestBody TeacherRegisterVo registerVo) {
        if (teacherService.save(new Teacher(registerVo))) {
            return RespBean.success("添加成功！");
        }
        System.out.println(registerVo.toString());
        return RespBean.error("添加失败！");
    }

    @ApiOperation(value = "删除老师")
    @DeleteMapping("/{id}")
    public RespBean deleteTeacher(@PathVariable Integer id) {
        if (teacherService.removeById(id)) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "更新老师")
    @PutMapping("/update")
    public RespBean updateTeacher(@RequestBody TeacherUpdateVo updateVo) {
        if (teacherService.updateById(new Teacher(updateVo))) {
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }
    @ApiOperation(value = "获取单个教师信息")
    @GetMapping("/{name}")
    public List<Teacher> getOneTeacher(@PathVariable String name){
        return teacherService.getOneTeacher(name);
    }
//    @ApiOperation(value = "获取班级数据")
//    @GetMapping(value = "/{classroom}")
//    public List<Teacher> getOneClass(@PathVariable("classroom") String classroom){
//        return teacherService.getOneClass(classroom);
//    }
}
